package study.nosql.redis.support.aop;

import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
//@Aspect
@Component
public class RedisTemplateAspect {

    @Around("target(org.springframework.data.redis.core.RedisTemplate)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Stopwatch stopwatch = Stopwatch.createStarted();

        //
        Signature signature = joinPoint.getSignature();
        String shortName = signature.toShortString();
        //
        Object[] args = joinPoint.getArgs();
        log.info("{}", args);

        try {
            return joinPoint.proceed();
        } catch (Throwable ex) {
            ex.printStackTrace();
            throw ex;
        } finally {
            log.info("{} cost time [{}] ms", shortName, stopwatch.elapsed(TimeUnit.MILLISECONDS));
        }
    }
}
